﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Xml.Linq;




namespace PHWebClient
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            WebClient wc = new WebClient();
            Uri uri = new Uri("http://localhost/PriceHistoryService/GetPriceHistory.aspx?ticker=MSFT&startdate=1-1-2009&enddate=1-10-2009", UriKind.RelativeOrAbsolute);
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(uri);
        }

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            StreamReader read = new StreamReader(e.Result);
            string strXml = read.ReadToEnd();
            XDocument xmlDoc = XDocument.Parse(strXml);
           // IEnumerable<XElement> test = xmlDoc.Descendants("TimeSeries");

            IEnumerable<TimeSeriesData> myTimeSeries = from item in xmlDoc.Descendants("TimeSeries")
                                                       select new TimeSeriesData
                                                      {
                                                        open = Convert.ToDouble(item.Element("Open").Value)
                                                        
   
                                                     };
            
            Dispatcher.BeginInvoke(() => _items.ItemsSource = myTimeSeries);
        }
    }
}
